home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / Live Scroll / Windows.c < prev   
Encoding:
C/C++ Source or Header  |  2000-09-28  |  18.4 KB  |  797 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Handle application's windows
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/6/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24.  
  25. #pragma segment Core
  26.  
  27.  
  28. #include <Sound.h>
  29. // System Includes
  30.  
  31. #ifndef __TYPES__
  32.     #include <Types.h>
  33. #endif
  34.  
  35. #ifndef __WINDOWS__
  36.     #include <Windows.h>
  37. #endif
  38.  
  39. #ifndef __DIALOGS__
  40.     #include <Dialogs.h>
  41. #endif
  42.  
  43. #ifndef __QUICKDRAW__
  44.     #include <Quickdraw.h>
  45. #endif
  46.  
  47. #ifndef __PICTUTILS__
  48.     #include <PictUtils.h>
  49. #endif
  50.  
  51. #ifndef __RESOURCES__
  52.     #include <Resources.h>
  53. #endif
  54.  
  55. #ifndef __FONTS__
  56.     #include <Fonts.h>
  57. #endif
  58.  
  59. #ifndef __TOOLUTILS__
  60.     #include <ToolUtils.h>
  61. #endif
  62.  
  63. #ifndef __ERRORS__
  64.     #include <Errors.h>
  65. #endif
  66.  
  67.  
  68.  
  69.  
  70. // Application Includes
  71.  
  72. #ifndef __BAREBONES__
  73.     #include "BareBones.h"
  74. #endif
  75.  
  76. #ifndef __PROTOTYPES__
  77.     #include "Prototypes.h"
  78. #endif
  79.  
  80.  
  81.  
  82.  
  83. // Static prototypes
  84. static OSErr        CreateDocumentWindow ( WindowRef* windowRef );
  85. static GWorldPtr    CreateOffscreen ( CTabHandle theCTabHndl, SInt16 theXsize, SInt16 theYsize,
  86.                                         SInt16 theBitDepth, GWorldFlags theFlags );
  87. static OSErr        DrawPictToOffscreen ( PicHandle thePictHndl, GWorldPtr theOffscreen );
  88. static OSErr        DrawOffscreenToWindow ( GWorldPtr theOffscreen, WindowPtr theWindow );
  89. static Rect            GetWindowVisibleRect ( WindowRef theWindow, SInt16 theSizeX, SInt16 theSizeY );
  90. static void            SaveSetMMUMode ( Boolean bIsSaveMode );
  91. static void            DrawClippedScrollBarLines ( WindowRef theWindow );
  92. static void            DrawClippedGrowIcon ( WindowRef theWindow );
  93. static void            HandleContentClick ( WindowRef theWindow, EventRecord* event );
  94. static OSErr        CreateWindowInfo ( WindowRef windowRef, Size infoSize );
  95. static pascal OSErr    SafeGetPictInfo ( PicHandle thePictHandle, PictInfo* thePictInfo,
  96.                                         SInt16 verb, SInt16 colorsRequested,
  97.                                         SInt16 colorPickMethod, SInt16 version );
  98. static void            SizeScrollBars ( WindowRef theWindow );
  99. static Point        GetMaximumWindowSize ( WindowRef theWindow );
  100.  
  101.  
  102.  
  103. // Default RGB Colors
  104.  
  105. static const RGBColor     kRGBBlack = {0x0000, 0x0000, 0x0000};
  106. static const RGBColor    kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. //
  114. // This is called to create the application's window.
  115. //
  116. void CreateWindow ( void )
  117. {
  118.     OSErr        theErr;
  119.     WindowRef    theWindow;
  120.     
  121.     theErr = CreateDocumentWindow ( &theWindow );
  122.     if ( theErr )
  123.         AlertUser ( kGenericErrorStr, theErr, "\p" );
  124.     
  125.     return;
  126. }
  127.  
  128.  
  129.  
  130. //
  131. // This will close the application's window, dispose of any storage we've hung
  132. // off the window, and then dispose of the window itself.
  133. //
  134. WindowRef DestroyWindow ( WindowRef windowRef )
  135. {
  136.     if ( windowRef )
  137.     {
  138.         tWindowInfoPtr    theInfo;
  139.         
  140.         theInfo = (tWindowInfoPtr) GetWRefCon ( windowRef );
  141.         if ( theInfo )
  142.         {
  143.             if ( theInfo->hScrollBar )
  144.                 DisposeControl ( theInfo->hScrollBar );
  145.             if ( theInfo->vScrollBar )
  146.                 DisposeControl ( theInfo->vScrollBar );
  147.                 
  148.             if ( theInfo->offscreen )
  149.                 DisposeGWorld ( theInfo->offscreen );                
  150.             
  151.             DisposePtr ( (Ptr) theInfo );
  152.         }
  153.         
  154.         DisposeWindow ( windowRef );
  155.         
  156.     }
  157.     
  158.     return nil;
  159. }
  160.  
  161.  
  162.  
  163. void DoActivate ( EventRecord* theEvent )
  164. {
  165.     Boolean            bActiveFlag = theEvent->modifiers & resumeFlag;
  166.     WindowRef        theWindow = (WindowRef) theEvent->message;
  167.     GrafPtr            savePort;
  168.     tWindowInfoPtr    theInfo;
  169.     
  170.     
  171.     gInBackground = (theEvent->modifiers & resumeFlag) == 0;
  172.     
  173.     GetPort ( &savePort );
  174.     SetPortWindowPort ( theWindow );
  175.     
  176.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  177.     
  178.     if ( bActiveFlag )
  179.     {
  180.         ShowControl ( theInfo->hScrollBar );
  181.         ShowControl ( theInfo->vScrollBar );
  182.     }
  183.     else
  184.     {
  185.         HideControl ( theInfo->hScrollBar );
  186.         HideControl ( theInfo->vScrollBar );
  187.         DrawClippedScrollBarLines ( theWindow );
  188.     }
  189.     
  190.     DrawClippedGrowIcon ( theWindow );
  191.     
  192.     SetPort ( savePort );
  193.     
  194.     return;
  195. }
  196.  
  197.  
  198.  
  199. void DoUpdate ( WindowRef theWindow )
  200. {
  201.     GrafPtr            savePort;
  202.     CGrafPtr        thePort;
  203.     
  204.     
  205.     thePort = GetWindowPort ( theWindow );
  206.     
  207.     GetPort ( &savePort );
  208.     SetPortWindowPort ( theWindow );
  209.     BeginUpdate ( theWindow );                    // visRgn temporarily = updateRgn
  210.     EraseRect ( &thePort->portRect );
  211.     
  212.     UpdateWindowContent ( theWindow );
  213.     
  214.     if ( gInBackground )
  215.         DrawClippedScrollBarLines ( theWindow );
  216.     else
  217.         UpdateControls ( theWindow, theWindow->visRgn );
  218.     DrawClippedGrowIcon ( theWindow );
  219.     
  220.     EndUpdate ( theWindow );                    // restore normal visRgn of grafport
  221.     SetPort ( savePort );
  222.     
  223.     return;
  224. }
  225.  
  226.  
  227.  
  228. void UpdateWindowContent ( WindowRef theWindow )
  229. {
  230.     tWindowInfoPtr    theInfo;
  231.     
  232.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  233.     DrawOffscreenToWindow ( theInfo->offscreen, theWindow );
  234.     
  235.     return;
  236. }
  237.  
  238.  
  239.  
  240. void DoContentClick ( WindowRef theWindow, EventRecord* theEvent )
  241. {
  242.     OSErr        theErr = noErr;
  243.     WindowRef    frontWindow;
  244.     
  245.     // If a movable modal is active, ignore click in an inactive 
  246.     // window, otherwise select it or handle the content click.
  247.     
  248.     frontWindow = FrontWindow ( );
  249.     if ( theWindow != frontWindow )
  250.     {
  251.         if ( IsMovableModal ( frontWindow ) )
  252.             SysBeep ( 30 );
  253.         else
  254.             SelectWindow ( theWindow );
  255.     }
  256.     else
  257.     {
  258.         SInt16            thePart;
  259.         GrafPtr            savePort;
  260.         ControlRef        theControl;
  261.         Point            localPt;
  262.     
  263.         localPt = theEvent->where;
  264.         GlobalToLocal ( &localPt );
  265.         thePart = FindControl ( localPt, theWindow, &theControl );
  266.         
  267.         GetPort ( &savePort );
  268.         SetPortWindowPort ( theWindow );
  269.         
  270.         if ( thePart )
  271.         {
  272.             switch ( thePart )
  273.             {
  274.                 case kControlUpButtonPart:
  275.                 case kControlDownButtonPart:
  276.                 case kControlPageUpPart:
  277.                 case kControlPageDownPart:
  278.                     TrackControl ( theControl, localPt, gScrollControlActionUPP );
  279.                 break;
  280.                 
  281.                 case kControlIndicatorPart:
  282.                     if ( BeginThumbTracking ( theControl ) == noErr )
  283.                     {
  284.                         TrackControl ( theControl, localPt,
  285.                                         (ControlActionUPP) gScrollThumbActionUPP );
  286.                         EndThumbTracking ( );
  287.                     }
  288.                 break;
  289.             }
  290.         }
  291.         else
  292.             HandleContentClick ( theWindow, theEvent );
  293.             
  294.         SetPort ( savePort );
  295.     }
  296.         
  297.     return;
  298.     
  299. } // DoContentClick
  300.  
  301.  
  302.  
  303. void DoGrowWindow ( WindowRef theWindow, EventRecord* theEvent )
  304. {
  305.     WindowPtr            savePort;
  306.     SInt32                returnCoord;
  307.     Rect                resizeBounds;
  308.     SInt16                theWidth,
  309.                         theHeight;
  310.     Point                theMaxSize;
  311.     
  312.     GetPort ( &savePort );
  313.     theMaxSize = GetMaximumWindowSize ( theWindow );
  314.     SetRect ( &resizeBounds, 96, 96, theMaxSize.h, theMaxSize.v );
  315.     returnCoord = GrowWindow ( theWindow, theEvent->where, &resizeBounds );
  316.     theHeight   = HiWord ( returnCoord );
  317.     theWidth = LoWord ( returnCoord );
  318.     
  319.     SizeWindow ( theWindow, theWidth, theHeight, true );
  320.     SizeScrollBars ( theWindow );
  321.     SetPort ( theWindow );
  322.     InvalRect ( &theWindow->portRect );
  323.     
  324.     SetPort ( savePort );
  325.     
  326.     
  327.     return;
  328. }
  329.  
  330.  
  331.  
  332. void DoDragWindow ( WindowRef theWindow, EventRecord* theEvent )
  333. {
  334.     WindowRef    frontWindow;
  335.     
  336.     
  337.     // If a movable modal is active, ignore click in an inactive 
  338.     // title bar, otherwise let the Window Manager handle it.
  339.     
  340.     frontWindow = FrontWindow ( );
  341.     if ( theWindow != frontWindow && IsMovableModal ( frontWindow ) )
  342.         SysBeep ( 30 );
  343.     else                                
  344.     {
  345.         RgnHandle    theRgn;
  346.         Rect        dragRect;
  347.         
  348.         theRgn = GetGrayRgn ( );
  349.         dragRect = (*theRgn)->rgnBBox;
  350.         DragWindow ( theWindow, theEvent->where, &dragRect );
  351.     }
  352.     
  353.     return;
  354. }
  355.  
  356.  
  357.  
  358. OSErr DoAboutBox ( void )
  359. {
  360.  
  361.     OSErr            theErr = noErr;
  362.     SInt16            theItem = 0;
  363.     GrafPtr            savePort = nil;
  364.     DialogRef        theDialog;
  365.     ModalFilterUPP    theFilter = nil;
  366.     
  367.     
  368.     theDialog = GetNewDialog ( kAboutDialog, nil, (WindowPtr) -1 );
  369.     
  370.     GetPort ( &savePort );
  371.     SetPort ( theDialog );
  372.  
  373.     ShowWindow ( theDialog );
  374.     
  375.     // Get the standard filter proc
  376.     theErr = GetStdFilterProc ( &theFilter );
  377.     if ( theErr )
  378.         goto CleanupAndBail;
  379.     
  380.     // Tell the dialog manager to use the default button
  381.     SetDialogDefaultItem ( theDialog, kStdOkItemIndex );
  382.       
  383.       
  384.     // Modal dialog loop    
  385.     do
  386.     {
  387.         // Use "theFilter" in ModalDialog call
  388.            ModalDialog ( theFilter, &theItem );
  389.            
  390.     } while ( theItem != kStdOkItemIndex );
  391.     
  392.     
  393. CleanupAndBail:
  394.     
  395.     DisposeDialog ( theDialog );
  396.     SetPort ( savePort );
  397.  
  398.     return theErr;
  399. }
  400.  
  401.  
  402.  
  403. //
  404. // Creates a document window containing a picture.
  405. //
  406. static OSErr CreateDocumentWindow ( WindowRef* windowRef )
  407. {
  408.     OSErr            theErr = noErr;
  409.     WindowRef        theWindow;
  410.     Point            theImageSize;
  411.     PicHandle        thePict = nil;
  412.     PictInfo        thePictInfo;
  413.     tWindowInfoPtr    theInfo;
  414.     
  415.     
  416.     
  417.     theWindow = GetNewCWindow ( kDisplayWindow, nil, (WindowRef) -1 );
  418.     if ( theWindow == nil )
  419.         return (ResError ( )) ? ResError ( ) : resNotFound;
  420.     
  421.     theErr = CreateWindowInfo ( theWindow, sizeof ( tWindowInfo ) );
  422.     if ( theErr )    goto CleanupAndBail;
  423.     
  424.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  425.     
  426.     SetPortWindowPort ( theWindow );
  427.     
  428.     thePict = GetPicture ( kPictureID );
  429.     if ( thePict == nil )    goto CleanupAndBail;
  430.     
  431.     theErr = SafeGetPictInfo ( thePict, &thePictInfo, returnColorTable, 256, systemMethod, 0 );
  432.     if ( theErr )    goto CleanupAndBail;
  433.     
  434.     theImageSize.h = thePictInfo.sourceRect.right - thePictInfo.sourceRect.left;
  435.     theImageSize.v = thePictInfo.sourceRect.bottom - thePictInfo.sourceRect.top;
  436.     
  437.     theInfo->offscreen = CreateOffscreen ( (thePictInfo.depth <= 8) ? thePictInfo.theColorTable : nil,
  438.                                             theImageSize.h, theImageSize.v, thePictInfo.depth,
  439.                                             kNoFlags );
  440.     if ( theInfo->offscreen == nil )    goto CleanupAndBail;
  441.     
  442.     theInfo->hScrollBar = GetNewControl ( kScrollBar, theWindow );
  443.     theInfo->vScrollBar = GetNewControl ( kScrollBar, theWindow );
  444.     if ( theInfo->hScrollBar == nil || theInfo->vScrollBar == nil)
  445.         goto CleanupAndBail;
  446.     
  447.     
  448.     // Setup the correct control values
  449.     SetControlMinimum ( theInfo->hScrollBar, 0 );
  450.     SetControlMinimum ( theInfo->vScrollBar, 0 );
  451.     SetControlValue ( theInfo->hScrollBar, 0 );
  452.     SetControlValue ( theInfo->vScrollBar, 0 );
  453.     
  454.     SizeScrollBars ( theWindow );
  455.     
  456.     theErr = DrawPictToOffscreen ( thePict, theInfo->offscreen );
  457.     if ( theErr )    goto CleanupAndBail;
  458.     
  459.     SelectWindow ( theWindow );
  460.     ShowWindow ( theWindow );
  461.     
  462.     *windowRef = theWindow;
  463.     
  464.     if ( thePict )
  465.         ReleaseResource ( (Handle) thePict );
  466.         
  467.     return noErr;
  468.     
  469. CleanupAndBail:
  470.     
  471.     // Don't forget to free any storage we've used so far
  472.     if ( thePict )
  473.         ReleaseResource ( (Handle) thePict );
  474.     
  475.     DestroyWindow ( theWindow );
  476.     
  477.     return theErr;
  478. }
  479.  
  480.  
  481.  
  482. static GWorldPtr CreateOffscreen ( CTabHandle theCTabHndl, SInt16 theXsize, SInt16 theYsize,
  483.                                     SInt16 theBitDepth, GWorldFlags theFlags )
  484. {
  485.     GWorldPtr    theGWorld = nil;
  486.     QDErr        theErr;
  487.     Rect        theRect;
  488.     
  489.     
  490.     SetRect ( &theRect, 0, 0, theXsize, theYsize );
  491.     theErr = NewGWorld ( &theGWorld, theBitDepth, &theRect, theCTabHndl, nil, theFlags );
  492.     
  493.     #if WARNINGS
  494.     if ( theErr )
  495.         DebugStrNum ( "\p CreateOffscreen: ", theErr );
  496.     #endif
  497.     
  498.     return theGWorld;
  499. }
  500.  
  501.  
  502.  
  503. static OSErr DrawPictToOffscreen ( PicHandle thePictHndl, GWorldPtr theOffscreen )
  504. {
  505.     PixMapHandle    theGWorldPMHndl;
  506.     GDHandle        saveGDevice;
  507.     CGrafPtr        saveGWorld;
  508.     OSErr            theErr = noErr;
  509.     
  510.     
  511.     GetGWorld ( &saveGWorld, &saveGDevice );
  512.     SetGWorld ( theOffscreen, nil );
  513.     
  514.     // We'll initialize out port settings here.
  515.     // If you don't set the foreground and background colors to black
  516.     // and white, colorisation can occur during the call to CopyBits.
  517.     RGBForeColor ( &kRGBBlack );                            
  518.     RGBBackColor ( &kRGBWhite );
  519.     // Reset the transfer mode
  520.     PenMode ( srcCopy );                                    
  521.     
  522.     
  523.     theGWorldPMHndl = GetGWorldPixMap ( theOffscreen );
  524.     LockPixels ( theGWorldPMHndl );
  525.     
  526.     EraseRect ( &theOffscreen->portRect );
  527.     // render the image into the offscreen buffer
  528.     HLock ( (Handle) thePictHndl );
  529.     DrawPicture ( thePictHndl, &theOffscreen->portRect );
  530.     HUnlock ( (Handle) thePictHndl );
  531.     
  532.     UnlockPixels ( theGWorldPMHndl );
  533.     SetGWorld ( saveGWorld, saveGDevice );
  534.     
  535.     return    theErr;
  536. }
  537.  
  538.  
  539.  
  540. static OSErr DrawOffscreenToWindow ( GWorldPtr theOffscreen, WindowPtr theWindow )
  541. {
  542.     OSErr            theErr = noErr;
  543.     CGrafPtr        savePort = nil;
  544.     GDHandle        saveGDevice;
  545.     PixMapHandle    thePixMapHndl = nil;
  546.     RGBColor        saveForeColor,
  547.                     saveBackColor;
  548.     Rect            sourceRect,
  549.                     destRect;
  550.     
  551.     
  552.     GetGWorld ( &savePort, &saveGDevice );
  553.     SetGWorld ( theOffscreen, nil );
  554.     GetForeColor ( &saveForeColor );
  555.     GetBackColor ( &saveBackColor );
  556.     
  557.     RGBForeColor ( &kRGBBlack );
  558.     RGBBackColor ( &kRGBWhite );
  559.     thePixMapHndl = GetGWorldPixMap ( theOffscreen );
  560.     if ( PixMap32Bit ( thePixMapHndl ) )                    // if 32bit mode needed == true
  561.         SaveSetMMUMode ( true );
  562.     if ( !LockPixels ( thePixMapHndl ) )
  563.         goto CleanupAndBail;
  564.         
  565.     sourceRect = theOffscreen->portRect;
  566.     destRect = theWindow->portRect;
  567.     destRect.right -= kScrollBarWidth;                        // exclude scrollbar area
  568.     destRect.bottom -= kScrollBarWidth;
  569.     if ( !EqualRect ( &sourceRect, &destRect ) )
  570.         sourceRect = GetWindowVisibleRect ( theWindow, destRect.right, destRect.bottom );
  571.     SetGWorld ( savePort, saveGDevice );
  572.     savePort = nil;
  573.     
  574.     CopyBits ( (BitMap*) *thePixMapHndl, (BitMap*) &theWindow->portBits,
  575.                     &sourceRect, &destRect, srcCopy, nil);
  576.     
  577.     
  578. CleanupAndBail:
  579.     
  580.     if ( savePort )
  581.         SetGWorld ( savePort, saveGDevice );
  582.     
  583.     RGBForeColor ( &saveForeColor );
  584.     RGBBackColor ( &saveBackColor );
  585.     UnlockPixels ( thePixMapHndl );
  586.     SaveSetMMUMode ( false );
  587.     
  588.     return theErr;
  589. }
  590.  
  591.  
  592.  
  593. static void DrawClippedScrollBarLines ( WindowRef theWindow )
  594. {
  595.     CGrafPtr    thePort;
  596.     Rect        theRect;
  597.     
  598.     
  599.     thePort = GetWindowPort ( theWindow );
  600.     theRect = thePort->portRect;
  601.     
  602.     MoveTo ( theRect.left, theRect.bottom - kScrollBarWidthAdjust );
  603.     LineTo ( theRect.right - kScrollBarWidthAdjust, theRect.bottom - kScrollBarWidthAdjust );
  604.     MoveTo ( theRect.right - kScrollBarWidthAdjust, theRect.top );
  605.     LineTo ( theRect.right - kScrollBarWidthAdjust, theRect.bottom - kScrollBarWidthAdjust );
  606.     
  607.     return;
  608. }
  609.  
  610.  
  611.  
  612. static void DrawClippedGrowIcon ( WindowRef theWindow )
  613. {
  614.     CGrafPtr    thePort;
  615.     RgnHandle    saveClip = nil;
  616.     Rect        newClip;
  617.     
  618.     
  619.     thePort = GetWindowPort ( theWindow );
  620.     saveClip = NewRgn ( );
  621.     GetClip ( saveClip );
  622.     
  623.     newClip = thePort->portRect;
  624.     newClip.top = newClip.bottom - kScrollBarWidth;
  625.     newClip.left = newClip.right - kScrollBarWidth;
  626.     ClipRect ( &newClip );
  627.     DrawGrowIcon ( theWindow );
  628.     
  629.     SetClip ( saveClip );
  630.     DisposeRgn ( saveClip );
  631.     
  632.     return;
  633. }
  634.  
  635.  
  636.  
  637. static Rect GetWindowVisibleRect ( WindowRef theWindow, SInt16 theSizeX, SInt16 theSizeY )
  638. {
  639.     Rect            displayRect;
  640.     SInt16            theValueX,
  641.                     theValueY;
  642.     tWindowInfoPtr    theInfo;
  643.     
  644.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  645.     theValueX = GetControlValue ( theInfo->hScrollBar );
  646.     theValueY = GetControlValue ( theInfo->vScrollBar );
  647.     SetRect ( &displayRect, theValueX, theValueY, theValueX + theSizeX, theValueY + theSizeY );
  648.     
  649.     return displayRect;
  650. }
  651.  
  652.  
  653.  
  654. static void    SaveSetMMUMode ( Boolean bIsSaveMode )
  655. {
  656.     static signed char        theAddrMode = true32b;
  657.     static Boolean            bSwapModeNeeded = false;
  658.     
  659.     if ( bIsSaveMode )
  660.     {
  661.         if ( GetMMUMode ( ) == false32b )            // get current addr mode
  662.         {
  663.             bSwapModeNeeded = true;                    // in 24 bit mode swap needed
  664.             SwapMMUMode ( &theAddrMode );            // switch to 32 bit mode
  665.         }
  666.     }
  667.     else if ( bSwapModeNeeded )
  668.         SwapMMUMode ( &theAddrMode );
  669.         
  670.     return;
  671. }
  672.  
  673.  
  674.  
  675. static void HandleContentClick ( WindowRef theWindow, EventRecord* event )
  676. {
  677.     #pragma unused(theWindow)
  678.     Point        localPt;
  679.     
  680.     localPt = event->where;
  681.     GlobalToLocal ( &localPt );
  682.     
  683.     // Handle any content clicks here
  684.     
  685.     return;
  686. }
  687.  
  688.  
  689.  
  690. //
  691. // Creates the storage for the data to hang off a window or dialog
  692. //
  693. static OSErr CreateWindowInfo ( WindowRef windowRef, Size infoSize )
  694. {
  695.     OSErr    theErr;
  696.     Ptr        theInfo = nil;
  697.     
  698.     
  699.     theInfo = NewPtrClear ( infoSize );
  700.     theErr = MemError ( );
  701.     if ( theErr )
  702.         return theErr;
  703.     
  704.     SetWRefCon ( windowRef, (long) theInfo );
  705.     
  706.     return noErr;
  707. }
  708.  
  709.  
  710.  
  711. //
  712. // The GetPictInfo routine has a bug which will allow all hell to
  713. // break loose if there isn't enough temporary memory available.
  714. //
  715. static pascal OSErr SafeGetPictInfo ( PicHandle thePictHandle, PictInfo* thePictInfo, SInt16 verb, SInt16 colorsRequested, SInt16 colorPickMethod, SInt16 version )
  716. {
  717.     const SInt32 kMinLowMem    = 10240;        // 10K isn't too much to ask for!
  718.     
  719.     if ( TempFreeMem ( )  < kMinLowMem )
  720.         return memFullErr;
  721.     
  722.     return GetPictInfo ( thePictHandle, thePictInfo, verb, colorsRequested, colorPickMethod, version );
  723. }
  724.  
  725.  
  726.  
  727. static void SizeScrollBars ( WindowRef theWindow )
  728. {
  729.     SInt16            maxXsize,
  730.                     maxYsize;
  731.     tWindowInfoPtr    theInfo;
  732.     
  733.     
  734.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  735.     
  736.     MoveControl ( theInfo->vScrollBar, theWindow->portRect.right - kScrollBarWidthAdjust,
  737.                                         theWindow->portRect.top - 1 );
  738.     MoveControl ( theInfo->hScrollBar, theWindow->portRect.left - 1, 
  739.                                         theWindow->portRect.bottom - kScrollBarWidthAdjust );
  740.     SizeControl ( theInfo->vScrollBar, kScrollBarWidth,
  741.                                         theWindow->portRect.bottom - theWindow->portRect.top - 13 );
  742.     SizeControl ( theInfo->hScrollBar, theWindow->portRect.right - theWindow->portRect.left - 13,
  743.                                         kScrollBarWidth );
  744.                                         
  745.     maxXsize = theInfo->offscreen->portRect.right - 
  746.                 ((theWindow->portRect.right - kScrollBarWidthAdjust) - theWindow->portRect.left);
  747.     maxYsize = theInfo->offscreen->portRect.bottom - 
  748.                 ((theWindow->portRect.bottom - kScrollBarWidthAdjust) - theWindow->portRect.top);
  749.     SetControlMaximum ( theInfo->vScrollBar, maxYsize );
  750.     SetControlMaximum ( theInfo->hScrollBar, maxXsize );
  751.     
  752.     return;
  753. }
  754.  
  755.  
  756.  
  757. //
  758. // Calculates the maximum window size based on the image size and the
  759. // current size of the main screen. The image size is used, but resticted
  760. // by the current screen size.
  761. //
  762. static Point GetMaximumWindowSize ( WindowRef theWindow )
  763. {
  764.     GDHandle            theMainGDevHndl;
  765.     Point                theWindowSize,
  766.                         theImageSize;
  767.     tWindowInfoPtr        theInfo;
  768.     
  769.     
  770.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  771.     
  772.     // Calulate the image size
  773.     theImageSize.h = theInfo->offscreen->portRect.right - theInfo->offscreen->portRect.left;
  774.     theImageSize.v = theInfo->offscreen->portRect.bottom - theInfo->offscreen->portRect.top;
  775.     theImageSize.h += kScrollBarWidth;
  776.     theImageSize.v += kScrollBarWidth;
  777.     
  778.     // Calulate the screen size
  779.     theMainGDevHndl = GetMainDevice ( );
  780.     theWindowSize.h = (*theMainGDevHndl)->gdRect.right - (*theMainGDevHndl)->gdRect.left;
  781.     theWindowSize.v = (*theMainGDevHndl)->gdRect.bottom - (*theMainGDevHndl)->gdRect.top;
  782.     theWindowSize.v -= GetMBarHeight ( );
  783.     
  784.     // Make sure the image size is within the constraints of the screen
  785.     theWindowSize.h = theWindowSize.h < theImageSize.h ? theWindowSize.h : theImageSize.h;
  786.     theWindowSize.v = theWindowSize.v < theImageSize.v ? theWindowSize.v : theImageSize.v;
  787.     
  788.     return theWindowSize;
  789. }
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.